<?php
/**
 * 
 * User(会员管理文件)
 *
 * @package      	YOURPHP
 * @author          liuxun QQ:147613338 <admin@yourphp.cn>
 * @copyright     	Copyright (c) 2008-2011  (http://www.yourphp.cn)
 * @license         http://www.yourphp.cn/license.txt
 * @version        	YourPHP企业网站管理系统 v2.1 2012-10-08 yourphp.cn $
 */
if(!defined("Yourphp")) exit("Access Denied");
class UserAction extends AdminbaseAction {

    public $dao,$usergroup;
	function _initialize()
	{
		parent::_initialize();
		$this->dao = D('User');
		$this->usergroup=F('Role');
		$this->assign('usergroup',$this->usergroup);
                 $this->assign('groupid',$_SESSION['groupid']); 
	}


	function index(){
		import ( '@.ORG.Page' );

		$keyword=$_GET['keyword'];
		$searchtype=$_GET['searchtype'];
		$groupid =intval($_GET['groupid']);

		$this->assign($_GET);
		
		if(!empty($keyword) && !empty($searchtype)){
			$where[$searchtype]=array('like','%'.$keyword.'%');
		}
		if($groupid)$where['groupid']=$groupid;
                if($_SESSION['groupid'] == 12 or $_SESSION['groupid'] == 13){
                    $where['operator'] = $_SESSION['userid'] ;
                }
		$user=$this->dao;
		$count=$user->where($where)->count();
		$page=new Page($count,20);
		$show=$page->show();
		$this->assign("page",$show);
		$list=$user->order('id desc')->where($where)
		->limit($page->firstRow.','.$page->listRows)->select();
                if(empty(s('username1'))){
                    $username1 = M('user')->getfield('id,realname') ;
                    s('username1',$username1,300);
                    
                }
                $this->assign('username1',s('username1'));
		$this->assign('ulist',$list);
		$this->display();
	}

	function insert(){
		$user=$this->dao;
		$_POST['password'] = sysmd5($_POST['pwd']);
                $_POST['operator'] = $_SESSION['userid'] ;
		if($data=$user->create()){
			if(false!==$user->add()){
				$uid=$user->getLastInsID();
				$ru['role_id']=$_POST['groupid'];
				$ru['user_id']=$uid;
				$roleuser=M('RoleUser');
				$roleuser->add($ru);			
				$this->success(L('add_ok'));
			}else{
				$this->error(L('add_error'));
			}
		}else{
			$this->error($user->getError());
		}
	}

	function update(){
		$user=$this->dao;
		$_POST['password'] = $_POST['pwd'] ? sysmd5($_POST['pwd']) : $_POST['opwd'];
		if(!empty($_POST['id'])){
				if(false!==$user->save($_POST)){
					$ru['user_id']=$_POST['id'];
					$ru['role_id']=$_POST['groupid'];
					$roleuser=M('RoleUser');
					$roleuser->where('user_id='.$_POST['id'])->delete();
					$roleuser->where('user_id='.$_POST['id'])->add($ru);
					$this->success(L('edit_ok'));
				}else{
					$this->error(L('edit_error').$user->getDbError());
				}
		}else{
				$this->error(L('do_error'));
		}
		 
	}
        
        function zhuanye() {
        $uid = $_GET['uid'];
        if ($_GET['level'] == 1) {
            $list = M('zhuanye')->field('id,title')->select();
            $this->assign('level', 1);
            $aczy = M('access_zhuanye')->where(array('uid' => $uid))->select();
        } else {
            if ($_SESSION['groupid'] == 12) {
                $where1['groupid'] = 12  ;
                $where1['id'] =  $_SESSION['userid'];
            } else {  
                $where1['groupid'] = 12 ;
            }
            $list = M('user')->field('id,realname as title')->where($where1)->select();
            foreach ($list as $key => $value) {
                $list[$key]['zy'] = M('access_zhuanye')->query("select z.id as zid ,az.zyid as zzyid,z.title as ztitle,az.uid as azschoolid from yourphp_access_zhuanye as az left join yourphp_zhuanye as z on az.zyid = z.id where az.uid = '" . $value['id'] . "' ");
            }
            $aczy = M('access_zhuanye')->where(array('uid' => $uid))->select();
        }

        $this->assign('aczy', $aczy);
        $this->assign('list', $list);
        $this->assign('uid', $uid);
        $this->display();
    }

    function savezhuanye() {
        $uid = $_POST['uid'];
        $aa = M('access_zhuanye')->where(array('uid' => $uid))->delete();
        if ($_POST['level'] == 1) {
            foreach ($_POST['nid'] as $key => $value) {
                $order['zyid'] = $value;
                $order['uid'] = $uid;
                $order['dateline'] = time();
                $bbb = M('access_zhuanye')->add($order);
            }
            if ($bbb) {
                $this->success('添加成功');
            }
        } else {
            if (empty($_POST['zyid']) || empty($_POST['scid'])) {
                $this->error('请选择学校和专业');
                exit;
            }
            foreach ($_POST['zyid'] as $kk => $vv) {
                $ueao = M('access_zhuanye')->where(array('zyid' => $vv, 'uid' => $_POST['scid'], 'schoolid' => 0))->find();
                if (empty($ueao)) {
                    break;
                    $this->error('请选择学校对应下的专业');
                    exit;
                }
            }
            foreach ($_POST['zyid'] as $kk => $vv) {
                $hhh['zyid'] = $vv;
                $hhh['uid'] = $_POST['uid'];
                $hhh['schoolid'] = $_POST['scid'];
                $hhh['dateline'] = time();
                $ppp = M('access_zhuanye')->add($hhh);
            }
            if ($ppp) {
                $this->success('添加成功');
            } else {
                $this->error('添加失败');
            }
        }
    }

    function _before_add(){
        $yourphp_auth_key = sysmd5(C('ADMIN_ACCESS').$_SERVER['HTTP_USER_AGENT']);
		$yourphp_auth = authcode('1-1-0-10-jpeg,jpg,png,gif-5-230', 'ENCODE',$yourphp_auth_key);
		$this->assign('yourphp_auth',$yourphp_auth);
		$this->assign('rlist',$this->usergroup);	
	}

	function _before_edit(){
            $yourphp_auth_key = sysmd5(C('ADMIN_ACCESS').$_SERVER['HTTP_USER_AGENT']);
		$yourphp_auth = authcode('1-1-0-10-jpeg,jpg,png,gif-5-230', 'ENCODE',$yourphp_auth_key);
		$this->assign('yourphp_auth',$yourphp_auth);
		$this->assign('rlist',$this->usergroup);	
	}


	function delete(){
		$id=$_GET['id'];
		$user=$this->dao;
		if(false!==$user->delete($id)){
			$roleuser=M('RoleUser');
			$roleuser->where('user_id ='.$id)->delete();
			delattach(array('moduleid'=>0,'catid'=>0,'id'=>0,'userid'=>$id));
			$this->success(L('delete_ok'));
		}else{
			$this->error(L('delete_error').$user->getDbError());
		}
	}

	function deleteall(){		
		$ids=$_POST['ids'];
		if(!empty($ids) && is_array($ids)){
			$user=$this->dao;
			$id=implode(',',$ids);
			if(false!==$user->delete($id)){
				$roleuser=M('RoleUser');
				$roleuser->where('user_id in('.$id.')')->delete();
				delattach("moduleid=0 and catid=0 and id=0 and userid in($id)");
				$this->success(L('delete_ok'));
			}else{
				$this->error(L('delete_error'));
			}
		}else{
			$this->error(L('do_empty'));
		}
	}
}
?>